<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <#include "/pubHeadStyle.html">
</head>
<body>
<div style="position: relative;height: 100%;padding: 10px;box-sizing: border-box;">
    <input type="hidden" name="actList">
        <div class="" id="flowListDiv">
            <table id="flowList" lay-filter="flowList" ></table>
        </div>
</div>
<style>
    div#LAY_app{
        overflow-x: hidden !important;
        overflow-y: auto !important;
    }
</style>
<script type="text/html" id="customHeaderTool">
    <div class="layui-btn-container" style="float: right;">
        <a class="layui-btn layui-btn-sm" lay-event="query" style="margin: 4px 0">
            <i class="layui-icon layui-icon-search"></i>
            <span>查询</span>
        </a>
    </div>
    <form class="layui-form" action="" style="margin-top:10px">
        <div class="table-top-filter" style="width: 580px">
            <div class="table-filter-alone">
                <label class="layui-form-label" style="width: 110px;">分类名称:</label>
                <input type="text" name="Q_typeName_SL" id="Q_typeName_SL" class="layui-input filter-value" placeholder="分类名称"
                       autocomplete="off"/>
            </div>
        </div>
    </form>
</script>
<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','table', 'layer', 'jquery', 'form','util', 'tree','commons', 'totalFilter', 'setter','admin','common'], function () {
        var table = layui.table, layer = layui.layer, setter = layui.setter, commons = layui.commons,
          totalFilter = layui.totalFilter, form = layui.form, $ = layui.$;
        var admin = layui.admin;
        var util = layui.util;
        var common = layui.common;

        if(!layui.actList) layui.actList = [];
        var defaultActList = [];
        var selectedList = [];
        if(layui.actList.length>0){
            defaultActList = defaultActList.concat(layui.actList);
            selectedList = selectedList.concat(layui.actList);
        }
        // 初始化
        renderTable('/system/globalType/flowTypeSelector');
        totalFilter.init();

        // endregion
        // region 渲染表格
        function renderTable(url) {
            table.render({
                elem: '#flowList'
                ,url: url
                ,title: '流程选择'
                ,height:'full-74'
                ,defaultToolbar: []
                ,toolbar: '#customHeaderTool'
                ,cols: [[
                    {type: 'checkbox', width: 80}
                    ,{field: 'typeName', title: '分类名称', align: 'center'}
                    // ,{field: 'typeName', title: '分类', align: 'center', width: 100}
                ]]
                ,page: true
                ,parseData: function (res) {
                    //回显已选择的流程分类
                    var _data = res.data;
                    if (_data.length > 0 && selectedList.length > 0) {
                        for (var i = 0; i < _data.length; i++) {
                            var _d = _data[i];
                            for (var j = 0; j < selectedList.length; j++) {
                                var _td = selectedList[j];
                                if (_d.typeId === _td.defKey) {
                                    _d.LAY_CHECKED = true;
                                }
                            }
                        }
                    }
                    // 渲染“已选择”列表
                    renderSelectedList();
                }
            });
        }
        $(document).on("click","#flowListDiv .layui-table-body table.layui-table tbody tr",function(){
            var obj = event ? event.target : event.srcElement;
            var tag = obj.tagName;
            var checkbox = $(this).find("td div.laytable-cell-checkbox div.layui-form-checkbox I");
            if(checkbox.length!=0){
                if(tag == 'DIV') {
                    checkbox.click();
                }
            }
        });
        $(document).on("click","td div.laytable-cell-checkbox div.layui-form-checkbox",function(e){
            e.stopPropagation();
        });
        // endregion
        // region 表格事件
        //表格头工具栏事件
        table.on('toolbar(flowList)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data; //获取选中行数据
            switch (obj.event) {
                case 'query':
                    table.reload("flowList", {
                        where: {
                            Q_typeName_SL: $("#Q_typeName_SL").val(),
                        },
                        page: {
                            curr: 1
                        }
                    });
                    break;
            }
        });
        // 表格checkbox事件
        table.on('checkbox(flowList)',function (obj) {
            layui.each(table.checkStatus('flowList').data,function(i,v){
                var index = indexOfActList(v.typeId);
                if(index === -1){
                    selectedList.push({defKey: v.typeId,defName: v.typeName});
                }
            });
            if(!obj.checked) {
                if(obj.type === 'all'){
                    layui.each(table.cache.flowList,function(i,v){
                        var index = indexOfActList(v.typeId);
                        selectedList.splice(index,1);
                    })
                }else{
                    var index = indexOfActList(obj.data.typeId);
                    selectedList.splice(index,1);
                }
            }
            $("input[name=actList]").val(JSON.stringify(selectedList));
            // 渲染“已选择”列表
            // renderSelectedList();
        });
        function indexOfActList(defKey){
            var i = -1;
            layui.each(selectedList,function(index,item){
                if(item.defKey === defKey){
                    i = index;
                    return true;
                }
            });
            return i;
        }
        // 渲染“已选择”列表函数
        function renderSelectedList(){
            $('.selectedList').html("");
            layui.each(selectedList,function(i,v){
                $('.selectedList').append('<a href="javascript:delSelected(\''+ v.defKey +'\');"><span>'+ v.defName +'</span><i class="layui-icon layui-icon-delete"></i></a>');
            });
            $("input[name=actList]").val(JSON.stringify(selectedList));
        }
        // 删除“已选择”项函数
        window.delSelected = function(defKey){
            selectedList.splice(indexOfActList(defKey),1);
            // 重新渲染“已选择”列表
            renderSelectedList();
            // 重新渲染流程列表表格
            table.reload('flowList',{
                parseData: function (res) {
                    var _data = res.data;
                    if (_data.length > 0 && selectedList.length > 0) {
                        for (var i = 0; i < _data.length; i++) {
                            var _d = _data[i];
                            for (var j = 0; j < selectedList.length; j++) {
                                var _td = selectedList[j];
                                if (_d.modelKey === _td.defKey) {
                                    _d.LAY_CHECKED = true;
                                }
                            }
                        }
                    }
                },
                page: {curr: 1}
            });
        }
        // endregion
        // region 按钮事件
        // 选择
        $("#select").click(function () {
            layui.actList = selectedList;
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        });
        // 取消
        $("#cancel").click(function () {
            layui.actList = defaultActList;
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        });
        // endregion
    });
</script>
<style>
    .selectedList a {
        cursor: pointer;
        position: relative;
        display: block;
        padding: 2px 5px;
    }
    .selectedList i {
        position: absolute;
        right: 10px;
    }
   .layui-table-tool-temp{
       padding: 0;
   }
    div#LAY_app{
        overflow: hidden;
    }
</style>
</body>
</html>